package io.jee.medusa.utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonRootName;

@JsonRootName("result")
public class ResultModel<T> implements Serializable {

	private static final long serialVersionUID = 1L;
	private boolean success;
	private Integer code;
	private String message;
	private T data;
	
	public ResultModel(int code, String message) {
		this(false, code, message, null);
	}
	
	public ResultModel(int code, String message, T data) {
		this(false, code, message, data);
	}
	
	public ResultModel(boolean success, T data) {
		this(success, null, null, data);
	}
	
	public ResultModel(boolean success, String message) {
		this(success, null, message, null);
	}
	
	public ResultModel(boolean success, String message, T data) {
		this(success, null, message, data);
	}
	
	public ResultModel(boolean success, Integer code, String message) {
		this(success, code, message, null);
	}
	
	public ResultModel(boolean success, Integer code, String message, T data) {
		this.success = success;
		this.code = code;
		this.message = message;
		this.data = data;
	}
	
	public Map<String, Object> resultMap(){
		Map<String, Object> result = new HashMap<>();
		result.put("success", success);
		if(code!=null){
			result.put("code", code);
		}
		result.put("message", message);
		if(data!=null){
			result.put("data", data);
		}
		return result;
	}
	
	public static <T> ResultModel<T> result(boolean success, String message){
		int code = 0;
		if(success){
			code = 1;
		}
		return new ResultModel<>(success, code, message);
	}
	
	public static <T> ResultModel<T> result(boolean success, int code, String message){
		return new ResultModel<>(success, code, message, null);
	}
	
	public static <T> ResultModel<T> result(boolean success, String message, T data){
		int code = 0;
		if(success){
			code = 1;
		}
		return new ResultModel<>(success, code, message, data);
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	@Override
	public String toString() {
		return "ResultModel [success=" + success + ", code=" + code
				+ ", message=" + message + ", data=" + data + "]";
	}
	
}
